<?php

App::uses('AdminController', 'Controller');

class AdminGroupsController extends AdminController {
    
    public $uses = array('Group');
    
    public function index() {
        
    }
    
    public function update_positions() {
        $this->autoRender = false;
    
        $positions = @$this->params->query['positions'];
        if (! empty($positions)) {
            foreach ($positions as $groupId => $position) {
                $this->Group->save(array(
                        'id'       => $groupId,
                        'position' => $position
                ));
            }
        }
    }
    
    public function save() {
        $data = $this->data;
        $message = '';
        if (! empty($data)) {
            $this->Group->create($data);
            
            if ($this->Group->validates()) {
                if ($this->Group->save($data)) {
                    die($this->Group->id);
                } else {
                    $message = 'Greška pri čuvanju podataka.';
                }
            } else {
                $message = 'Naziv grupe nije validan!';
            }
        }
        
        header('HTTP/1.0 GET 404');
        echo $message;
    }
    
    public function delete($id) {
        if ($this->Group->delete($id)) {
            $this->setFlash('Grupa je uspešno obrisana.', 'ok');
        } else {
            $this->setFlash('Greška pri brisanju grupe!');
        }
        $this->redirect('/admin_products');
    }
    
    public function setHome($home, $groupId) {
        if ($this->Group->save(array(
            'id'   => $groupId,
            'home' => $home
        ))) {
            $this->setFlash('Grupa je uspešno ažurirana', 'ok');
        } else {
            $this->setFlash('Greška u ažuriranju grupe!');
        }
        
        $this->redirect('/admin_products/index/'.$groupId);
    }
    
}